#!/bin/bash
HERE=`pwd`
TMPBUILD=${HERE}/tmpbuild
TMPBUILD_BIS=${HERE}/tmpbuild_bis
#----------------------------------------------------------
if [ ! -e ${TMPBUILD}/cloonix/cloonix_config ]; then
  echo NOT FOUND:
  echo ${TMPBUILD}/cloonix/cloonix_config
  exit
fi
#----------------------------------------------------------
if [ ! -e ./cloonix/cloonix_config ]; then
  echo NOT FOUND:
  echo cloonix_config
  exit
fi
CLOONIX_VERSION=$(cat ./cloonix/cloonix_config | grep CLOONIX_VERSION)
CLOONIX_VERSION=${CLOONIX_VERSION#*=}
#----------------------------------------------------------
mkdir -p ${TMPBUILD_BIS}
cp -rf ${TMPBUILD}/cloonix* ${TMPBUILD_BIS}
#----------------------------------------------------------
CLOONIX_COMMON=${TMPBUILD_BIS}/cloonix/common
CLOONIX_SERVER=${TMPBUILD_BIS}/cloonix/server
CLOONIX_CLIENT=${TMPBUILD_BIS}/cloonix/client
#----------------------------------------------------------
  for i in allclean cli cmd libmdl libxwycli srv; do
    rm -rf ${CLOONIX_COMMON}/xwy/${i}
  done
#----------------------------------------------------------
  for i in cloonix_agent cmd cmd_hwsim_i386 \
           cmd_i386 hwsim allclean cloonix_dropbear \
           cmd_alien cmd_hwsim_x86_64  cmd_x86_64; do
    rm -rf ${CLOONIX_COMMON}/agent_dropbear/${i}
  done
#----------------------------------------------------------
  for i in cmd allclean spice_protocol spice_usb \
           virglrenderer get_new_spice_protocol.sh \
           get_new_usbredir.sh  get_new_virglrenderer.sh; do
    rm -rf ${CLOONIX_COMMON}/spice/${i}
  done
#----------------------------------------------------------
  for i in spice share spice_client spice-protocol \
           spice_server usbredir include virglrenderer ; do
    rm -rf ${CLOONIX_COMMON}/spice/spice_lib/${i}
  done
#----------------------------------------------------------
  rm -rf ${CLOONIX_COMMON}/glob_include
#----------------------------------------------------------
  for i in lib_io_clownix lib_rpc_doors lib_rpct \
           lib_rpc_c2c lib_rpc_layout lib_utils \
           lib_blkd lib_rpc_clownix lib_rpc_qmonitor \
           lib_zor ; do
    rm -rf ${CLOONIX_COMMON}/${i}
  done
#----------------------------------------------------------
  for i in  muswitch/lib_ioc \
            muswitch/lib_mulan \
            muswitch/lib_muend \
            muswitch/munat/clo_tcp \
            spice ; do
    rm -rf ${CLOONIX_SERVER}/${i}
  done
#----------------------------------------------------------
  for i in  doorways \
            uml_cloonix_switch \
            muswitch/mutap \
            muswitch/musnf \
            muswitch/munat \
            muswitch/muc2c \
            muswitch/mua2b \
            muswitch/mutst \
            muswitch/muovs \
            muswitch/muwlan \
            muswitch/mulan ; do
    rm -f ${CLOONIX_SERVER}/${i}/cmd
    rm -f ${CLOONIX_SERVER}/${i}/Makefile
    rm -rf ${CLOONIX_SERVER}/${i}/obj
    rm -rf ${CLOONIX_SERVER}/${i}/src
    rm -rf ${CLOONIX_SERVER}/${i}/include
  done
#----------------------------------------------------------
  rm -f ${CLOONIX_SERVER}/qemu/cmd
  rm -rf ${CLOONIX_SERVER}/qemu/sources
  rm -rf ${CLOONIX_SERVER}/qemu/qemu_bin/tainted_qemu
#----------------------------------------------------------
  rm -f ${CLOONIX_SERVER}/dtach/cmd
  rm -f ${CLOONIX_SERVER}/dtach/dtach_source.tar.gz
#----------------------------------------------------------
for i in allclean cmd dpdk ovs dpdk_mkpatch dpdk.patch \
         dpdk_patched_create dpdk.sh ovs_mkpatch \
         ovs.patch ovs_patched_create ovs.sh ring_client; do
    rm -rf ${CLOONIX_SERVER}/dpdk/${i}
  done
#----------------------------------------------------------
  rm -rf ${CLOONIX_CLIENT}/lib_client
  rm -rf ${CLOONIX_CLIENT}/lib_zor
  rm -rf ${CLOONIX_CLIENT}/spice
#----------------------------------------------------------
  for i in crcanvas_gtk Makefile obj src allclean cmd ; do 
    rm -rf ${CLOONIX_CLIENT}/cairo_canvas/${i}
  done
#----------------------------------------------------------
  for i in  ctrl \
            xwycli \
            qmonitor \
            hyperzor ; do
    rm -f ${CLOONIX_CLIENT}/${i}/Makefile
    rm -rf ${CLOONIX_CLIENT}/${i}/obj
    rm -rf ${CLOONIX_CLIENT}/${i}/include
    rm -rf ${CLOONIX_CLIENT}/${i}/src
    rm -rf ${CLOONIX_CLIENT}/${i}/cmd
    rm -rf ${CLOONIX_CLIENT}/${i}/allclean
  done
#----------------------------------------------------------
  for i in tainted_openssh lib_doorways ; do
    rm -rf ${CLOONIX_CLIENT}/openssh/$i
  done
  for i in all_clean cmd mkpatch openssh* patched_create ; do
    rm -f ${CLOONIX_CLIENT}/openssh/$i
  done
#----------------------------------------------------------
cd ${TMPBUILD_BIS}
tar zcvf ../cloonix-${CLOONIX_VERSION}.tar.gz .
rm -rf ${TMPBUILD_BIS}
#----------------------------------------------------------
